<?php
namespace Home\Controller;

use Home\Model\Admin;
use Think\Controller;

/**
 *处理登录注销等功能
 * @author Qiu Pengfei
 * @since 1.0
 */
class DefaultController extends Controller
{
    //处理登录请求
    public function login()
    {
        if (!empty($_POST)) {
            $username = isset($_POST['username']) ? $_POST['username'] : '';
            $password = isset($_POST['username']) ? $_POST['password'] : '';
            if (Admin::login($username, $password)) {
                //登录成功
                $returnUrl = session('returnUrl');
                if (!empty($returnUrl)) {
                    //登录拦截时记录的url (哪里来，回哪里)
                    header('Location:' . $returnUrl);
                } else {
                    //跳去admin的首页
                    $this->redirect('Index/index');
                }
                return;
            }
            //登录失败
            $this->assign('message', '用户名或密码不正确!');
        }
        //显示登录页面
        $this->display();
    }
    //注销登录
    public function logout()
    {
        session('Home',null);
        $this->redirect('login');
    }


}